package com.example.baselibrary.Utils;

import android.util.Base64;

import org.junit.Test;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import static org.junit.Assert.*;

public class InstructUtilsTest1 {


    private final static String HEX = "0123456789ABCDEF";
    private final static String TRANSFORMATION = "DES/CBC/PKCS5Padding";//DES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
    private final static String IVPARAMETERSPEC = "01020304";////初始化向量参数，AES 为16bytes. DES 为8bytes.
    private final static String ALGORITHM = "DES";//DES是加密方式
    private static final String SHA1PRNG = "SHA1PRNG";//// SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法


    @Test
    public void personNumber() {
        int s = 9999;
        StringBuffer stringBuffer = new StringBuffer();
        if (s < 100) {
            stringBuffer.append("2A");
            stringBuffer.append("09");
            stringBuffer.append("01");
            stringBuffer.append("00");
            if (s < 10) {
                stringBuffer.append(0);
                stringBuffer.append(s + " ");
            } else {
                stringBuffer.append(s + " ");
            }
            stringBuffer.append("00");
            stringBuffer.append("00");
            stringBuffer.append("02");
            stringBuffer.append("2F");

        } else if (100 <= s && s <= 9999) {
            stringBuffer.append("2A");
            stringBuffer.append("09");
            stringBuffer.append("01");

            if (s <= 999) {
                stringBuffer.append(0);
                stringBuffer.append(s + "");
            } else {
                stringBuffer.append(s + "");
            }
            stringBuffer.append("00");
            stringBuffer.append("00");
            stringBuffer.append("02");
            stringBuffer.append("2F");
        }
    }

    @Test
    public void showPersonNumber() {
        String s = "2A1106100100000000002F";
        String number = s.substring(6, s.length());
        number = number.substring(0, 4);
        System.out.println(number);

    }


    /**
     * DES算法，加密
     *
     * @param data 待加密字符串
     * @param key  加密私钥，长度不能够小于8位
     * @return 加密后的字节数组，一般结合Base64编码使用
     */
    @Test
    public  void encodes() {
        try {
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            IvParameterSpec iv = new IvParameterSpec(IVPARAMETERSPEC.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, getRawKey("!6@#E_$R"), iv);
            byte[] bytes = cipher.doFinal("郑一飞".getBytes());
            System.out.println(Base64.encodeToString(bytes, Base64.DEFAULT));
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    // 对密钥进行处理
    private static Key getRawKey(String key) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
        //for android
        SecureRandom sr = null;
        // 在4.2以上版本中，SecureRandom获取方式发生了改变
        if (android.os.Build.VERSION.SDK_INT >= 17) {
            sr = SecureRandom.getInstance(SHA1PRNG, "Crypto");
        } else {
            sr = SecureRandom.getInstance(SHA1PRNG);
        }
        // for Java
        // secureRandom = SecureRandom.getInstance(SHA1PRNG);
        sr.setSeed(key.getBytes());
        kgen.init(64, sr); //DES固定格式为64bits，即8bytes。
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return new SecretKeySpec(raw, ALGORITHM);
    }


}